package cn.hxzy.servlet.admin;

import cn.hxzy.entity.News;
import cn.hxzy.entity.User;
import cn.hxzy.service.NewsService;
import cn.hxzy.service.UserService;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Date;
import java.util.List;

@WebServlet("/admin/addNews")
public class AddNewsServlet extends HttpServlet {
    NewsService newsService = new NewsService();
    UserService userService = new UserService();

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            req.getSession().getAttribute("loginUser");
            List<News> all = newsService.findAll();
            req.setAttribute("addNews", all);
            req.getRequestDispatcher("/WEB-INF/jsp/admin/addNews.jsp").forward(req, resp);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        try {
            String id = req.getParameter("id");
            String name = req.getParameter("name");
            String newsValue = req.getParameter("newsValue");
//            User user = new User();
            System.out.println(id+"\n*************");
            newsService.add(new News(name, newsValue, new Date(), Integer.parseInt(id)));
            resp.sendRedirect("showNews");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
